package com.itany.boot.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.boot.constant.DictConstant;
import com.itany.boot.constant.StatusConstant;
import com.itany.boot.dao.CommentMapper;
import com.itany.boot.entity.Comment;
import com.itany.boot.exception.RequestParameterErrorException;
import com.itany.boot.service.CommentService;
import com.itany.boot.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @date 2025/10/13
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<Comment> findByParams(Comment commentParam, String pageNo, String pageSize) {
        if (ParameterUtil.isNull(pageNo)){
            pageNo = DictConstant.COMMENT_DEFAULT_PAGE_NO;
        }
        if(ParameterUtil.isNull(pageSize)){
            pageSize = DictConstant.COMMENT_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        List<Comment> comments = commentMapper.selectByParams(commentParam);
        PageInfo<Comment> commentPage = new PageInfo<>(comments);
        return commentPage;
    }

    @Override
    public Comment findById(String id) throws RequestParameterErrorException {
        if(ParameterUtil.isNull(id)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        return commentMapper.selectByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public void modifyStatus(String id, String status) throws RequestParameterErrorException {
        if(ParameterUtil.isNull(id)||ParameterUtil.isNull(status)){
            throw new RequestParameterErrorException("请求参数有误");
        }
        Comment comment=new Comment();
        comment.setId(Integer.parseInt(id));
        comment.setStatus(Integer.parseInt(status));
//        if(comment.getStatus()== DictConstant.COMMENT_STATUS_ENABLED){
//            comment.setStatus(DictConstant.COMMENT_STATUS_DISABLED);
//        }else{
//            comment.setStatus(DictConstant.COMMENT_STATUS_ENABLED);
//        }
        commentMapper.updateByPrimaryKeySelective(comment);

    }
}
